home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Calculators / NumberCrunch 1.41 / Number Crunch II Demo / Library Routines / Text Examples / Interpolation, Min, Zeros < prev    next >
Encoding:
Text File  |  1992-09-03  |  7.6 KB  |  234 lines  |  [TEXT/NCII]

  1. ################
  2. # Interpolation, Minima, and Zeros.
  3. #
  4. #   function Interpolate(X,Y, Xinterp)  # Returns Yinterp[1…J] at Xinterp[1…J] which agrees with X[1…N], Y[1…N].
  5. #
  6. #   function Zero(f, xLeft,xRight) # Returns x between Left,Right such that f(x)=0.
  7. #
  8. #   function Minimize(f, x, xLeft,xRight) # Returns minimum value of f(x).
  9. #  This text file explains and gives examples of
  10. #  some of the routines in the file All Library Routines, which
  11. #  should be Opened before trying any of these examples.
  12. #
  13. #################
  14.  
  15. ########
  16. # Here are the xCOD's that are used.
  17.  
  18. xINTERPOLATE
  19.   # xCOD xINTERPOLATE(N:num; X,Y,dy2,w2:RealArray; Ni:num; Xi,Y, Err:num), interpolates y[1…N] of x[1…n] to the points Xi[1…ni]. dy2[1…N],w2[1…N] are work arrays. Output is Yi[1…Ni]. Err: 0=> no error, -1=> two x[j] are identical; an external program.
  20.  
  21. xZERO
  22.   # xCOD xZERO( prog F(n:num; xy:array); x,xLeft,xRight, tolerance, Err:num), Finds a zero of y(x), where F(2, [x,y]) calculates y. Input:guess for x,ragne xLeft,xRight, tolerance for x. Output:x. Err: -1=> x not in Left,Right range.; an external program.
  23.  
  24. xMINIMIZE
  25.   # xCOD xMINIMIZE( prog F(n:num; xy:array); x ,xL,xR, tol, err:num), finds min of y(x), where F(2, [x,y]) does y(x). In: guess for x, xL<x<xR, y(x) < y(xL) or y(xR), tolerance. Out: x. Err:-1=> x not >xL,<xR; -2=> f(x) > f(xL or xR); an external program.
  26.  
  27.  
  28. #############
  29. # Here are the interface routines.
  30.  
  31. # Given arrays y,x such that
  32. #   1) x is ordered, and
  33. #   2) y=f(x) for some function f, then
  34. # Interpolate returns the points Yinterp = f(Xinterp) for
  35. # an array (or single number) Xinterp. 
  36. # (Which need not be the same size as x or y.)
  37. #
  38. # The first work array, w1, actually returns the
  39. # 2nd derivatives of y(x), which are used in this cubic
  40. # spline interpolation.
  41. #
  42.   function Interpolate(X,Y, Xinterp)  # Returns Yinterp[1…J] at Xinterp[1…J] which agrees with X[1…N], Y[1…N].
  43. .   var n, w1,w2, Yinterp, err
  44. .  #  Input:
  45. .  #             x[1…n] ordered array
  46. .  #             y[1…n] array, y=f(x)
  47. .  #             Xinterp[1…J] co-ordinates to interpolate f(x) at.
  48. .  #  Output:
  49. .  #             Interpolate = an array =f( xInterp[1…J]  )
  50. .   begin
  51. .     w1=y; w2=y;   # reserve storage space.
  52. .     n = size(y)
  53. .     Yinterp = Xinterp
  54. .     Ni = size(Xinterp)
  55. .     xINTERPOLATE(n,x,y,w1,w2, Ni, Xinterp,Yinterp, err)
  56. .     if err<>0 then Print("ERROR in INTERP, two identical x values")
  57. .     Interpolate = Yinterp
  58. .   end
  59. .   
  60.  
  61. # Given a one-line NCII text function f
  62. #  (for example:  f(x) = 2 exp( sin(x)) - 3.2 )
  63. # Zero returns x s.t. f(x) = 0.
  64. #
  65.   function Zero(f, xLeft,xRight) # Returns x between Left,Right such that f(x)=0.
  66. .   var Tol, x, err
  67. .  #  Input:
  68. .  #            f = NCII user function like f(x) = 3x-1.
  69. .  #            xLeft, xRight = numbers bracketing x where f(x)=0.
  70. .  #  Output:
  71. .  #             value of x where f(x) = 0.
  72. .   begin
  73. .     fZeroFunc__ = f     # put function into one zzFuncForZero__ uses.
  74. .     Tol = 1e-6;     #desired tolerance of answer
  75. .     x = (xLeft+xRight)/2   # first guess for x
  76. .     xZero(zzFuncForZero__, x, xLeft, xRight, Tol, err)
  77. .     if err<>0 then Print(" ERROR IN ZERO = "+err)
  78. .     Zero = x
  79. .   end
  80. .   
  81.  
  82. #   Passing functions to external routines is tricky. 
  83. # The ONLY allowed format for an NCII routine is 
  84. # one that looks like this, where
  85. # n is the size of the array xy.  
  86. #   This one has xy=[x,y], and calculuates y=f(x).
  87.  procedure zzFuncForZero__(n,xy) # Used by function Zero.
  88. .   begin
  89. .     xy(2) = fZeroFunc__(xy(1))
  90. .   end
  91. .   
  92.  
  93. # Given another one-line NCII text function f
  94. # Minimize returns the minimum value of f(x)
  95. # inside xLeft, xRight.  This particular interface
  96. # to xMINIMIZE only works if the function value 
  97. # at the midpoint of xLeft, xRight is lower than 
  98. # the function at the ends.
  99. # x is changed to the location of the min, and
  100. # the height f(x) of the minimu is returned.
  101. #
  102.   function Minimize(f, x, xLeft,xRight) # Returns minimum value of f(x).
  103. .   var Tol, err
  104. .  #  Input:
  105. .  #             f = NCII user function
  106. .  #             xLeft, xRight = numbers bracketing the minimum
  107. .  #  Output:
  108. .  #              x such that f(x) is the min.
  109. .  #              Minimize = f(x) = minimum y value.
  110. .   begin
  111. .     x = (xLeft+xRight)/2  # start at midpt.
  112. .     fMinFunc__ = f
  113. .     Tol=1e-6
  114. .     xMINIMIZE(zzFuncForMin__, x,xLeft,xRight, Tol, err)
  115. .     if err=-1 then
  116. .       Minimize="ERROR IN MIN: x must be between xLeft,xRight"
  117. .     else if err=-2 then
  118. .        Minimize="ERROR IN MIN: f(x) >= f(xLeft) or f(xRight)"
  119. .     else if err=-3 then
  120. .        Minimize="ERROR IN MIN: iteration maximum hit"
  121. .     else
  122. .        Minimize=f(x)
  123. .      end
  124. .      
  125.  
  126. #   Procedure to caclulate the min function for the xCOD.
  127.  procedure zzFuncForMin__(n,xy) # Used by function Minimize.
  128. .   begin
  129. .     xy(2) = fMinFunc__(xy(1))
  130. .   end
  131. .   
  132.  
  133.  
  134. ################
  135. # Examples:
  136.  
  137. # First, interpolation.
  138. # Say we have these values of x,y:
  139. x = 0…2 @ .2;  y = cos(x);  
  140. #  table(x,y)   # Evaluating this without the first "#"  gave the table. 
  141. # x                  y
  142. # - - - - - - - - - - - - - - - 
  143.   0                  1
  144.   0.2               0.98007
  145.   0.4               0.92106
  146.   0.6               0.82534
  147.   0.8               0.69671
  148.   1                  0.5403
  149.   1.2               0.36236
  150.   1.4               0.16997
  151.   1.6               -0.0292
  152.   1.8               -0.2272
  153.   2                  -0.41615
  154.  
  155. # To find the values of this function at other x values,
  156. # (pretending for a moment that we don't know it's a cosine)
  157. xOther = [.15, .9, 1.7];    # say we want y(x) at these values.
  158. yOther = Interpolate(x,y, xOther)
  159.   yOther[1…3] = [0.98877, 0.62161, -0.12884] 
  160.  
  161. # The real values of the function should be 
  162. cos(xOther)
  163.   [0.98877, 0.62161, -0.12884] 
  164. # which are pretty durn close.
  165.  
  166.  
  167. # Now, for the zero of a function.
  168. pi = Zero(sin, 3, 3.2)   # find where sin is zero. (This is really at π).
  169.   pi = 3.14159272 
  170. # This is pretty close to the real value:
  171. (π-pi)
  172.   -6.5177165e-8    # This is the error; Zero is set for 1e-6 tolerance.
  173.  
  174.  
  175.  
  176. ########
  177. # Second: find where sin(x)=cos(x)^2.
  178. f(x) = sin(x) - cos(x)^2;      # Define the function we want to examine.
  179. # First we need to find a range where this goes through zero.
  180. x = 0…π@.2; y = f(x);
  181. # table(x,y)   # Evaluating the "Table(x,y)" gives this table:
  182. # x                  y
  183. # - - - - - - - - - - - - - - - 
  184.   0                  -1
  185.   0.2               -0.76186
  186.   0.4               -0.45894
  187.   0.6               -0.11654
  188.   0.8               0.23196
  189.   1                  0.54954
  190.   1.2               0.80074
  191.   1.4               0.95656
  192.   1.6               0.99872
  193.   1.8               0.92223
  194.   2                  0.73612
  195.   2.2               0.46216
  196.   2.4               0.13171
  197.   2.6               -0.21876
  198.   2.8               -0.55279
  199.   3                  -0.83897
  200. # From which we can see that the function is zero between
  201. # .6 and .8, as well as between 2.4 and 2.6. Of course, we could
  202. # have done this with the graph, too.
  203. #
  204. # Then
  205. alpha = Zero(f, .6, .8)
  206.   alpha = 0.66623943 
  207. # Try it:
  208. sin(alpha)
  209.   0.61803399 
  210. [cos(alpha)]^2
  211.   0.61803399 
  212. # They look pretty close…
  213.  
  214.  
  215.  
  216. ############
  217. # Finally, the minimum of a function.
  218. # Usually these should be graphed to find values xLeft, xRight 
  219. # which surround the minumum.
  220. f(x) = 3 + (x-1)^2;      # here's the function to minimize.
  221. # This has a min at f(1)=3.  Try 0.8, 1.3 as brackets.
  222. Minimize(f, xLow, 0.8, 1.3)        # Find the min. 
  223.   3      # This is the returned minimum value.
  224. xLow  # and here where the minimum occurs.
  225.   xLow = 0.9999994 
  226. |xLow-1|/xLow            # The fractional error in x is
  227.    5.960594e-7            # just under 1e-6, which was the 
  228.                                     # the tolerance set in Minimize.
  229.  
  230.